package com.sg.business.model.input;

import java.util.List;

import org.jbpm.task.Status;

import com.mobnut.db.model.AccountInfo;
import com.mobnut.db.model.PrimaryObject;
import com.mongodb.BasicDBObject;
import com.sg.business.model.IEngineeringChange;
import com.sg.business.model.UserTask;
import com.sg.business.model.Work;
import com.sg.widgets.commons.model.EditorInputFactory;
import com.sg.widgets.part.CurrentAccountContext;

public class WorkEditorInputFactory extends EditorInputFactory {

	public WorkEditorInputFactory(PrimaryObject primaryObject) {
		super(primaryObject);
	}

	@Override
	protected String getEditorId(PrimaryObject primaryObject, Object data) {
		if (data instanceof String) {
			String key = (String) data;
			if ("deliverable".equals(key)) {
				return "navigator.view.work.deliverable";
			}
		}
		Work work = (Work) primaryObject;
		if (work.isProjectWBSRoot()) {
			return "navigator.view.project";
		}
		Object internaltype = work.getValue(Work.F_INTERNAL_TYPE);
		if (Work.INTERNAL_TYPE_CHANGE.equals(internaltype)) {
			AccountInfo accountInfo = new CurrentAccountContext()
					.getAccountInfo();
			if (accountInfo != null) {
				if (work.countReservedAndInprogressUserTasks(accountInfo
						.getConsignerId()) > 0) {
					List<UserTask> userTasks = work
							.getAllUnChangedUserTasks(new BasicDBObject()
									.append("$ne", Status.Completed.name()));
					UserTask userTask = userTasks.get(0);
					if (IEngineeringChange.TASKNAME_APPLICATION.equals(userTask
							.getTaskName())) {
						return "navigator.work.engineeringchange.application";
					} else if (IEngineeringChange.TASKNAME_BUSINESS
							.equals(userTask.getTaskName())) {
						return "navigator.work.engineeringchange.business";
					}
					return "navigator.work.engineeringchange.unenable";
				}
			}
			return "navigator.work.engineeringchange";
		} else if (Work.INTERNAL_TYPE_PROJECTCHANGE.equals(internaltype)) {
			Object value = work.getValue(Work.F_PROJECT_CHANGE_EDITORABLE);
			if (Boolean.TRUE.equals(value)) {
				return "navigator.work.projectchange";
			} else {
				return "navigator.work.projectchange.1";
			}
		}

		if (work.isSummaryWork()) {
			if (Work.STATUS_ONREADY_VALUE.equals(work.getLifecycleStatus())) {
				return "navigator.view.work.4";
			} else if (Work.STATUS_WIP_VALUE.equals(work.getLifecycleStatus())) {
				if (work.isExecuteWorkflowActivateAndAvailable()) {
					return "navigator.view.work.6";
				} else {
					return "navigator.view.work.5";
				}
				// return "navigator.view.work.5";
			} else {
				return "navigator.view.work.2";
			}
		} else {
			if (Work.STATUS_ONREADY_VALUE.equals(work.getLifecycleStatus())) {
				return "navigator.view.work.1";
			} else {
				if (work.isExecuteWorkflowActivateAndAvailable()) {
					return "navigator.view.work.2";
				} else {
					return "navigator.view.work.3";
				}
			}
		}
	}
}

// package com.sg.business.model.input;
//
// import com.mobnut.commons.util.Utils;
// import com.sg.business.model.Work;
// import com.sg.widgets.Widgets;
// import com.sg.widgets.commons.model.IEditorInputFactory;
// import com.sg.widgets.part.editor.PrimaryObjectEditorInput;
// import com.sg.widgets.registry.config.Configurator;
// import com.sg.widgets.registry.config.DataEditorConfigurator;
//
// public class WorkEditorInputFactory implements IEditorInputFactory {
//
// private Work work;
//
// public WorkEditorInputFactory(Work work) {
// this.work = work;
// }
//
// @Override
// public PrimaryObjectEditorInput getInput(Object data) {
// DataEditorConfigurator conf = getEditorConfig(data);
// PrimaryObjectEditorInput editorInput = new PrimaryObjectEditorInput(
// work, conf, null);
// editorInput.setEditable(false);
// return editorInput;
// }
//
// @Override
// public DataEditorConfigurator getEditorConfig(Object data) {
//
// String editorId = Widgets.getEditorIdUsedBy(work,data);
// if(!Utils.isNullOrEmpty(editorId)){
// Configurator conf = Widgets.getEditorRegistry().getConfigurator(editorId);
// if(conf!=null){
// return (DataEditorConfigurator) conf;
// }
// }
//
// if (work.isSummaryWork()) {
// if (Work.STATUS_ONREADY_VALUE.equals(work.getLifecycleStatus())) {
// editorId = "navigator.view.work.4";
// } else if (Work.STATUS_WIP_VALUE.equals(work.getLifecycleStatus())) {
// editorId = "navigator.view.work.5";
// } else {
// editorId = "navigator.view.work.2";
// }
// } else {
// if (Work.STATUS_ONREADY_VALUE.equals(work.getLifecycleStatus())) {
// editorId = "navigator.view.work.1";
// } else {
// if (work.isExecuteWorkflowActivateAndAvailable()) {
// editorId = "navigator.view.work.2";
// } else {
// editorId = "navigator.view.work.3";
// }
// }
// }
//
// DataEditorConfigurator conf = (DataEditorConfigurator) Widgets
// .getEditorRegistry().getConfigurator(editorId);
// return conf;
// }
// }
